package com.example.secondhandshop.model;

import android.util.Log; // 确保导入了Log类

import com.example.secondhandshop.bean.ImageResponseData;
import com.example.secondhandshop.bean.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUploader {

    private static final String UPLOAD_URL = "https://api-store.openguet.cn/api/member/tran/image/upload";
    private static final MediaType MEDIA_TYPE_IMAGE = MediaType.parse("image/*");
    private static final String APP_ID = "2de64479320b423cbc05dd7e6c922999";
    private static final String APP_SECRET = "16786e5aa3e8a7f8e4f24940cb351fad802ca";

    public void uploadFile(File file, UploadCallback callback) {
        OkHttpClient client = new OkHttpClient();

        // 创建文件数组，实际使用时可能需要上传多个文件
        List<File> fileList = new ArrayList<>();
        fileList.add(file);

        // 构建请求体
        MultipartBody.Builder multipartBuilder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        for (File f : fileList) {
            multipartBuilder.addFormDataPart(
                    "fileList", f.getName(),
                    RequestBody.create(MEDIA_TYPE_IMAGE, f)
            );
        }
        MultipartBody multipartBody = multipartBuilder.build();

        // 构建请求
        Request request = new Request.Builder()
                .url(UPLOAD_URL)
                .post(multipartBody)
                .build();

        // 添加请求头参数
        Headers headers = new Headers.Builder()
                .add("appld", APP_ID)
                .add("appSecret", APP_SECRET)
                .build();

        request = request.newBuilder()
                .headers(headers)
                .build();

        // 发起异步请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("FileUploader", "onFailure: " + e.getMessage());
                callback.onUploadFailure("图片上传失败: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    callback.onUploadFailure("上传失败: " + response.message());
                    return;
                }

                String responseString = response.body().string();
                Gson gson = new Gson();
                ResponseBody<ImageResponseData> responseBody = gson.fromJson(responseString,
                        new TypeToken<ResponseBody<ImageResponseData>>(){}.getType());

                if (responseBody != null && responseBody.getCode() == 200) {
                    ImageResponseData imageResponseData = responseBody.getData();
                    if (imageResponseData != null) {
                        callback.onUploadSuccess(imageResponseData);
                    } else {
                        callback.onUploadFailure("响应数据中没有找到图片信息");
                    }
                } else {
                    if (responseBody != null) {
                        callback.onUploadFailure(responseBody.getMsg());
                    } else {
                        callback.onUploadFailure("解析响应数据失败");
                    }
                }
            }
        });
    }

    public interface UploadCallback {

        void onUploadSuccess(ImageResponseData imageResponseData);
        void onUploadFailure(String error);
    }

}